package com.test.controller.cw;

import com.test.controller.Admininfo.ResData;
import com.test.entity.BxMsg;
import com.test.entity.CwInfo;
import com.test.service.BxMsgService;
import com.test.service.CwInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpSession;
import java.util.Map;

@Controller
public class CwController {
    @Autowired
    private CwInfoService cwInfoService;
    @Autowired
    private BxMsgService bxMsgService;

//    显示cw页面
    @GetMapping("cw/index")
    public String getCw(){
        return "cw/index";
    }
//    显示财务信息
    @GetMapping("cw/cw_info/detail")
    public ModelAndView getCwInfoDetail(HttpSession session){
        ModelAndView modelAndView = new ModelAndView("cw/personal");
        Map login =(Map) session.getAttribute("login");
        Integer id =(Integer) login.get("id");
        CwInfo cwInfo = cwInfoService.queryById(id);
        modelAndView.addObject("detail",cwInfo);
        return modelAndView;
    }
//    显示报销页面
    @GetMapping("cw/bx_msg")
    public String getBxMsg(){
        return "cw/bx_msg/list";
    }
//    显示报销信息
    @GetMapping("cw/bx_msg/queryList")
    @ResponseBody
    public ResData getBxMsgQueryList(Integer page, BxMsg bxMsg){
        return bxMsgService.queryByPage(bxMsg, PageRequest.of(page-1,10));
    }
//    提交报销通过
    @GetMapping("cw/bx_msg/tg")
    public ResData getBxMsgTg(Integer id){
        ResData resData = new ResData();

        BxMsg bxMsg = bxMsgService.queryById(id);
        bxMsg.setBxStatus(2);
        BxMsg update = bxMsgService.update(bxMsg);
        if (update.getId()!=null){
            resData.setCode(1);
            resData.setMsg("审核通过");
            return resData;
        }
        resData.setCode(0);
        resData.setMsg("审核失败");
        return resData;
    }
//    跳转报销不通过页面
    @GetMapping("cw/bx_msg/btg")
    public ModelAndView getBxMsgBtg(Integer id){
        ModelAndView modelAndView = new ModelAndView("cw/bx_msg/btg_page");
        BxMsg bxMsg = bxMsgService.queryById(id);
        modelAndView.addObject("data",bxMsg);
        return modelAndView;
    }
//    提交报销不通过信息
    @PostMapping("cw/bx_msg/btg_submit")
    @ResponseBody
    public ResData postBxMsgBtgSubmit(Integer id,String checkRemark){
        ResData resData = new ResData();
        BxMsg bxMsg = bxMsgService.queryById(id);
        bxMsg.setBxStatus(3);
        bxMsg.setCheckRemark(checkRemark);
        BxMsg update = bxMsgService.update(bxMsg);
        if (update.getId()!=null) {
            resData.setCode(200);
            resData.setMsg("提交成功");
            return resData;
        }
        resData.setCode(0);
        resData.setMsg("提交失败");
        return resData;
    }

}
